package br.com.portal.dao;

import java.sql.Connection;
import java.sql.SQLException;

import br.com.portal.beans.Vendedor;
import br.com.portal.factory.ConnectionFactory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class VendedorDAO {
	public static Vendedor logar(String login, String senha) throws Exception {
		
		Vendedor vendedor = null;
		Connection conn = ConnectionFactory.getConnection();

		String sql = "SELECT * FROM vendedor WHERE login = ? AND senha = ?";

		PreparedStatement stmt = conn.prepareStatement(sql);
		stmt.setString(1, login.toUpperCase());
		stmt.setString(2, senha.toUpperCase());
		
		ResultSet rs = stmt.executeQuery();

		if(rs.next()){
			vendedor = new Vendedor();
			vendedor.setCodigo(rs.getString("codigo"));
			vendedor.setNome(rs.getString("nome"));
			vendedor.setLogin(rs.getString("login"));
			vendedor.setSenha(rs.getString("senha"));
			vendedor.setEmail(rs.getString("email"));
			vendedor.setTodosClientes(rs.getBoolean("todos_clientes"));
			vendedor.setAdmin(rs.getBoolean("admin"));
		}

		stmt.close();
		rs.close();
		conn.close();
		return vendedor;
	}
}